package cliVet.DAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.classic.Session;

import cliVet.classesBasicas.Animal;
import cliVet.interfaces.IAnimalDAO;

public class AnimalDAO implements IAnimalDAO {

	private Session session;

	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public void inserirAnimal(Animal animal) {

		this.session.save(animal);
	}

	@Override
	public void atualizarAnimal(Animal animal) {

		this.session.update(animal);
	}

	@Override
	public void excluirAnimal(Animal animal) {

		this.session.delete(animal);

	}

	@Override
	public Animal bucarAnimalNome(String nome) {

		String hql = "select a from Animal a where a.nome = :nome";
		Query consultaNome = this.session.createQuery(hql);
		consultaNome.setString(nome, nome);
		return (Animal) consultaNome.list();
	}

	@Override
	public Animal buscarAnimalCodigo(int codigo) {

		String hql = "select a from Animal a where a.codigo = :codigo";
		Query consultaNome = this.session.createQuery(hql);
		consultaNome.setInteger(codigo, codigo);
		return (Animal) consultaNome.uniqueResult();
	}

	@Override
	public Animal carregarAnimal(int codigo) {
		return (Animal) this.session.get(Animal.class, codigo);
	}

	@Override
	public List<?> listarAnimal() {

		return this.session.createCriteria(Animal.class).list();
	}

}
